/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package servlets;

import beans.*;
import dao.OrderDao;
import dao.OrderDetailDao;
import java.util.ArrayList;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Calendar;
import java.util.Date;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author appi
 */
@WebServlet(name = "ConfirmOrderAction", urlPatterns = {"/ConfirmOrderAction.do"})
public class ConfirmOrderAction extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            // To get Delievery date (DelDate)
            Calendar origDay = Calendar.getInstance();  // Gives current date
            origDay.add(Calendar.DAY_OF_YEAR, 3);       // Delivery date is 3 days after current date

            Date d = origDay.getTime();
            java.sql.Date sq = new java.sql.Date(d.getTime());
            // getTime() belongs to java.util package but we require it
            // to use java.sql.Date so have to provide downcasting here
            // nakho chho util, kadho chho sql :P LOL
            HttpSession sn1 = request.getSession();

            ArrayList<item> cart = (ArrayList<item>) sn1.getAttribute("cart");
            Custemer user = (Custemer) sn1.getAttribute("user");
            
            // Another way of above line : (Khtla vachhe)
            //Custemer use =new Custemer();
            //use=(Custemer)sn1.getAttribute("user");
            float finaltotal=0;
            float total = 0;
            for (item i : cart) {
              total = i.getIprice() * i.getIqty();
              finaltotal=finaltotal+total;
            }
            
            
            
            sn1.setAttribute("tot", finaltotal);
           
            
            // Once confirmed, all the detail of customer needs to be set in
            // order. So we will be dealing with "order" further more.
            
            Order o = new Order();

            o.setEmail(user.getCemail());
            o.setShipadd((String) sn1.getAttribute("shipadd"));
            o.setTotal(finaltotal);
            o.setDeldate(sq);
            sn1.setAttribute("deldate", sq);
            int oid=OrderDao.addOrder(o);
            
            OrderDetail od = new OrderDetail();
            for(item i:cart){
                od.setOid(oid);
                od.setIid(i.getId());
                od.setIqty(i.getIqty());
                
               // OrderDetailDao odd= new OrderDetailDao();
                OrderDetailDao.addOrderDetail(od);
            }
            response.sendRedirect("Receipt.jsp");
            

        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
